package com.sphd.restaurantsystem.activitys.staff;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.StaffAdapter;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.info.StaffInfo;
import com.sphd.restaurantsystem.presenter.StaffPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.StaffView;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/*
* 员工管理
* */
public class StaffActivity extends BaseActivity implements StaffView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    StaffPresenterImpl staffPresenter;
    int curpage = 1;
    StaffAdapter staffAdapter;
    @BindView(R.id.staff_add)
    ImageView staffAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staff);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> finish());
        mySwf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                curpage = 1;
                addData();
            }
        });

        staffAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                curpage++;
                addData();
            }
        });
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {


                bundle.putBoolean(Constant.ISHAS, isHas((List<StaffInfo>) baseQuickAdapter.getData()));
                bundle.putSerializable(Constant.STAFFINFO, (StaffInfo) baseQuickAdapter.getItem(i));
                bundle.putInt(Constant.TYPE, 1);
                intent.putExtras(bundle);
                intent.setClass(context, StaffInfoActivity.class);
                startActivityForResult(intent,1);
            }


            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                StaffInfo staffInfo = (StaffInfo) baseQuickAdapter.getItem(position);
                if (staffInfo.getStaff_auth() == 3) {
                    toast("店长不能在这里编辑");
                } else {
                    showDelDialog(staffInfo);
                }
            }
        });
    }

    private void showDelDialog(StaffInfo staffInfo) {
        AlertDialog.Builder delestaff = new AlertDialog.Builder(context);
        delestaff.setTitle("删除").setMessage("确定要删除" + staffInfo.getStaff_name() + "吗?").setCancelable(false)
                .setIcon(R.mipmap.delpeople)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                staffPresenter.delUrl(RestaurantUrl.Httptitle + RestaurantUrl.DelStaff + "staffid=" + staffInfo.getId());

                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
        // create alert dialog
        AlertDialog alertDialog = delestaff.create();

        // show it
        alertDialog.show();
    }

    @Override
    public void recRes(int res) {
        if (res == 1) {
            toast("删除成功");
            curpage = 1;
            addData();
        } else {
            toast("删除失败，请重试！");
        }
    }

    private boolean isHas(List<StaffInfo> data) {

        for (StaffInfo staffInfo : data) {
            if (staffInfo.getStaff_auth() == 4) {
                return true;
            }
        }
        return false;
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        setTitleBar(toolbar, "员工管理");
        staffPresenter = new StaffPresenterImpl(this);
        initAdapter();
        addData();
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecy);
        staffAdapter = new StaffAdapter(R.layout.item_vip, new ArrayList<>());
        myRecy.setAdapter(staffAdapter);
        staffAdapter.openLoadMore(10);
        staffAdapter.openLoadAnimation();
    }

    private void addData() {

        staffPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.ShowStaff + "cgid=" + restaurantInfo.getId() + "&curpage=" + curpage + "&pageno=10");
    }

    @Override
    public void recList(List<StaffInfo> staffInfoList) {
        mySwf.setRefreshing(false);
        if (curpage == 1) {
            staffAdapter.setNewData(staffInfoList);
        } else {
            staffAdapter.addData(staffInfoList);
        }
        if (staffInfoList.size() != 10) {
            staffAdapter.loadComplete();
        }
    }

    @OnClick(R.id.staff_add)
    public void onClick() {


        if (isCanDo()) {
            toast("店长会去操作，您不用费心了");
        } else {
            bundle.putBoolean(Constant.ISHAS, isHas((List<StaffInfo>) staffAdapter.getData()));
            bundle.putInt(Constant.TYPE, 0);
            intent.putExtras(bundle);
            intent.setClass(context, StaffInfoActivity.class);
            startActivityForResult(intent,1);

        }
    }

    private boolean isCanDo() {


        return (isCg_has_storemanager(restaurantInfo) && isSuperManager(loginInfo));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==1){
            curpage=1;
            addData();
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
        mySwf.setRefreshing(false);
    }
}
